package cn.com.bluemoon.daps.model.core.upload;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.concurrent.NotThreadSafe;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author Jarod.Kong
 */
@Slf4j
@NotThreadSafe
public class DapModelImportExeclDataListenerV2 extends AnalysisEventListener<LinkedHashMap<Integer, String>> {
    private final List<TableInfo> collector;
    List<List<TableInfo.Row>> tableInfos = new ArrayList<>();
    List<TableInfo.Row> tableRows = new ArrayList<>();
    boolean tableSegmentStart = false, fieldSegmentStart = false;
    private boolean tbStart = false, tbEnd = false;

    public DapModelImportExeclDataListenerV2(List<TableInfo> collector) {
        this.collector = collector;
    }

    /**
     * 这个每一条数据解析都会来调用
     */
    @Override
    public void invoke(LinkedHashMap<Integer, String> row, AnalysisContext context) {
        log.info("解析到一条数据:{}", JSON.toJSONString(row));
        if (row.isEmpty()) {
            return;
        }
        if ("</表>".equals(row.get(0))) {
            tbEnd = true;
            tbStart = false;
        }
        if ("<表>".equals(row.get(0))) {
            tbStart = true;
            tbEnd = false;
            return;
        }
        if (tbStart) {
            Integer rowIndex = context.readRowHolder().getRowIndex();
            List<TableInfo.Cell> tableRow = new ArrayList<>();
            int i = 0;
            Collection<String> cells = row.values();
            for (String val : cells) {
                tableRow.add(new TableInfo.Cell(i, val));
                i++;
            }
            if (row.size() == 4 && "物理模型||逻辑模型||表业务描述||主题分类".equals(String.join("||", cells))) {
                tableSegmentStart = true;
                fieldSegmentStart = false;
                return;


            }

            if (row.size() == 7 && "字段名称||字段英文名||字段类型||是否主键||是否为空||缺省||备注".equals(String.join("||", cells))) {
                tableSegmentStart = false;
                fieldSegmentStart = true;
                return;
            }
            TableInfo.Row tRow;
            if (tableSegmentStart && row.size() <= 4) {
                // 补充列数
                int currentTableRowSize = tableRow.size();
                if (currentTableRowSize < 4) {
                    for (int j = currentTableRowSize; j < 4; j++) {
                        tableRow.add(new TableInfo.Cell(j, null));
                    }
                }
                tRow = new TableInfo.Row(1, rowIndex, tableRow);
                tableSegmentStart = false;
            } else if (fieldSegmentStart && row.size() <= 7) {
                // 补充列数
                int currentTableRowSize = tableRow.size();
                if (currentTableRowSize < 7) {
                    for (int j = currentTableRowSize; j < 7; j++) {
                        tableRow.add(new TableInfo.Cell(j, null));
                    }
                }
                tRow = new TableInfo.Row(2, rowIndex, tableRow);
            } else {
                tRow = new TableInfo.Row(-1, rowIndex, tableRow);
            }
            tableRows.add(tRow);
        }
        if (tbEnd) {
            tableInfos.add(new ArrayList<>(tableRows));
            tableRows.clear();
        }
    }

    /**
     * 所有数据解析完成了 都会来调用
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("所有数据解析完成！");
        List<TableInfo> allTable = new ArrayList<>();
        for (List<TableInfo.Row> tableInfo : tableInfos) {
            TableInfo tb = new TableInfo();
            for (TableInfo.Row row : tableInfo) {
                if (row.getType() == 1) {
                    List<TableInfo.Cell> cells = row.getCells();
                    TableInfo.Table table = new TableInfo.Table(row.getRowIndex(),
                            cells.get(0), cells.get(1), cells.get(2), cells.get(3));
                    tb.setTable(table);
                } else if (row.getType() == 2) {
                    List<TableInfo.Cell> cells = row.getCells();
                    tb.getFieldInfos().add(new TableInfo.FieldInfo(row.getRowIndex(),
                            cells.get(0), cells.get(1), cells.get(2), cells.get(3), cells.get(4), cells.get(5), cells.get(6)));
                }
            }
            allTable.add(tb);
        }
        log.info("表结构化");
        collector.addAll(allTable);
    }
}

